<?php

class HerogroupModel extends Model {

	//获取所有阵型
    public static function getherogroups($uid = 0)
	{
		$uid = intval($uid);
		if(!$uid) return array('ecode' => ERR302);

		$heros = daocall("herogroup", "getherogroups", array($uid));
        if (!is_array($heros)) return array('ecode' => ERR508);

        return array('ecode' => 0, 'ret' => $heros);
	}

	//开启阵型
    public static function newherogroup($uid = 0, $gid = 0)
	{
		$uid = intval($uid);
		$gid = intval($gid);
		if(!$uid || !$gid) return array('ecode' => ERR302);

		$uext = daocall("user", "getuserext", array($uid));
        if (!$uext || !is_array($uext)) return array('ecode' => ERR402);
		$uext["lv"] = modcall("user", "xp2lv", array($uext["xp"]));
		
		$conf = &load_conf("pub:herogroup");
		$conf = &$conf['herogroup'];
		if(!$conf || !$conf[$gid]) return array('ecode' => ERR509);

		$conf = $conf[$gid]["lv"][1];
		if(intval($conf["ulv"]) > $uext["lv"]) return array('ecode' => ERR405);
		if(intval($conf["price"]) > intval($uext["price"])) return array('ecode' => ERR406);
		if(intval($conf["gb"]) > intval($uext["gb"])) return array('ecode' => ERR404);

		$uext["gb"] -= intval($conf["gb"]);
		if(!daocall("user", "setuserext", array($uid, array("gb" => $uext["gb"])))) return array('ecode' => ERR407);
		if(!daocall("herogroup", "newherogroup", array($uid, $gid, array("lv" => 1, "grid" => array($conf["heropos"] => 0))))) return array('ecode' => ERR510);

		return array("ecode" => SUCCESS);
    }

	//升级阵型
	public static function upherogroup($uid = 0, $gid = 0)
	{
		$uid = intval($uid);
		$gid = intval($gid);
		if(!$uid || !$gid) return array('ecode' => ERR302);

		$uext = daocall("user", "getuserext", array($uid));
        if (!$uext || !is_array($uext)) return array('ecode' => ERR402);
		$uext["lv"] = modcall("user", "xp2lv", array($uext["xp"]));

		$hg = daocall("herogroup", "getherogroup", array($uid, $gid));
		if(!$hg || !is_array($hg)) return array('ecode' => ERR508);
		$nlv = $hg["lv"] + 1;

		$conf = &load_conf("pub:herogroup");
		$conf = &$conf['herogroup'];
		if(!$conf || !$conf[$gid] || !$conf[$gid]["lv"][$nlv]) return array('ecode' => ERR509);

		$conf = $conf[$gid]["lv"][$nlv];
		if(intval($conf["ulv"]) > $uext["lv"]) return array('ecode' => ERR405);
		if(intval($conf["price"]) > intval($uext["price"])) return array('ecode' => ERR406);
		if(intval($conf["gb"]) > intval($uext["gb"])) return array('ecode' => ERR404);

		$uext["gb"] -= intval($conf["gb"]);
		$hg["grid"] = $hg["grid"] ? $hg["grid"] : array();
		$hg["grid"][$conf["heropos"]] = 0;
		$hg["lv"] = $nlv;
		if(!daocall("user", "setuserext", array($uid, array("gb" => $uext["gb"])))) return array('ecode' => ERR407);
		if(!daocall("herogroup", "setherogroup", array($uid, $gid, array("lv" => $hg["lv"], "grid" => $hg["grid"])))) return array('ecode' => ERR510);

		return array("ecode" => SUCCESS);
	}

	//MB升级阵型
	public static function mbupherogroup($uid = 0, $gid = 0)
	{
		$uid = intval($uid);
		$gid = intval($gid);
		if(!$uid || !$gid) return array('ecode' => ERR302);

		$uext = daocall("user", "getuserext", array($uid));
        if (!$uext || !is_array($uext)) return array('ecode' => ERR402);
		$uext["lv"] = modcall("user", "xp2lv", array($uext["xp"]));

		$hg = daocall("herogroup", "getherogroup", array($uid, $gid));
		if(!$hg || !is_array($hg)) return array('ecode' => ERR508);
		$nlv = $hg["lv"] + 1;

		$conf = &load_conf("pub:herogroup");
		$conf = &$conf['herogroup'];
		if(!$conf || !$conf[$gid] || !$conf[$gid]["lv"][$nlv]) return array('ecode' => ERR509);

		$conf = $conf[$gid]["lv"][$nlv];
		if(intval($conf["ulv"]) > $uext["lv"]) return array('ecode' => ERR405);
		if(!intval($conf["bfmb"])) return array('ecode' => ERR511);
		if(intval($conf["bfmb"]) > $uext['mb']) return array('ecode' => ERR403);

		$uext["mb"] -= intval($conf["bfmb"]);
		$hg["grid"] = $hg["grid"] ? $hg["grid"] : array();
		$hg["grid"][$conf["heropos"]] = 0;
		$hg["lv"] = $nlv;
		if(!daocall("user", "setuserext", array($uid, array("mb" => $uext["mb"])))) return array('ecode' => ERR407);
		if(!daocall("herogroup", "setherogroup", array($uid, $gid, array("lv" => $hg["lv"], "grid" => $hg["grid"])))) return array('ecode' => ERR510);

		return array("ecode" => SUCCESS);
	}

	//排列阵型
	public static function setherogroup($uid = 0, $gid = 0, $grid = array())
	{
		$uid = intval($uid);
		$gid = intval($gid);
		if(!$uid || !$gid || !$grid || !is_array($grid)) return array('ecode' => ERR302);

		$uext = daocall("user", "getuserext", array($uid));
        if (!$uext || !is_array($uext)) return array('ecode' => ERR402);
		$uext["lv"] = modcall("user", "xp2lv", array($uext["xp"]));

		$hg = daocall("herogroup", "getherogroup", array($uid, $gid));
		if(!$hg || !is_array($hg)) return array('ecode' => ERR508);

		$hg["grid"] = $hg["grid"] ? $hg["grid"] : array();
		foreach($grid as $gridno => $gridhid)
		{
			if(!array_key_exists($gridno, $hg["grid"])) continue;

			$hg["grid"][$gridno] = $gridhid;
		}

		if(!daocall("herogroup", "setherogroup", array($uid, $gid, array("grid" => $hg["grid"])))) return array('ecode' => ERR510);

		return array("ecode" => SUCCESS);
	}

	//设置当前阵型
	public static function activeherogroup($uid = 0, $gid = 0)
	{
		$uid = intval($uid);
		$gid = intval($gid);
		if(!$uid || !$gid) return array('ecode' => ERR302);

		$uext = daocall("user", "getuserext", array($uid));
        if (!$uext || !is_array($uext)) return array('ecode' => ERR402);
		$uext["lv"] = modcall("user", "xp2lv", array($uext["xp"]));

		$hg = daocall("herogroup", "getherogroup", array($uid, $gid));
		if(!$hg || !is_array($hg)) return array('ecode' => ERR508);

		if(!daocall("herogroup", "setactiveherogroup", array($uid, $gid))) return array('ecode' => ERR510);
		return array("ecode" => SUCCESS);
	}

}
?>